package com.zsrt.highschool.view;

import java.util.List;
import java.util.Scanner;

import com.zsrt.highschool.entity.Admin;
import com.zsrt.highschool.entity.Examinee;
import com.zsrt.highschool.service.ExamineeService;

public class HighSchoolView {
	
	private Scanner scanner;
	
	private ExamineeService examineeService;

	
	public HighSchoolView() {
		this.scanner = new Scanner(System.in);
		this.examineeService = new ExamineeService();
	}
	/** 
	 * 显示主菜单
	 */
	public void showMainmenu(){
		System.out.println("------------欢迎来到高考成绩查询系统--------");
		while(true){
			System.out.println("请选择以下操作：1.管理员登录 2.考生登录 3.退出");
			int operate = scanner.nextInt();
			if(operate==1){
				adminLogin();
				break;
			}else if(operate==2){
				userLogin();
				break;
			}else if(operate==3){
				examineeService.save();
				System.out.println("谢谢使用");
				break;
			}else{
				break;
			}
		}
	}
	/**
	 * 显示管理员菜单
	 */
	public void showAdminMenu(){
		System.out.println("请选择....");
		 while(true){
			 printSeparator();
			 System.out.println("1.录入学生信息");
			 System.out.println("2.修改学生信息");
			 System.out.println("3.删除学生信息");
			 System.out.println("4.查询所有学生信息");
			 System.out.println("5.根据户籍所在地查询学生信息");
			 System.out.println("6.根据名字查询学生信息");
			 System.out.println("7.根据学生的准考证号查询学生信息");
			 System.out.println("8.返回");
			 printSeparator();
			 int operate=scanner.nextInt();
			 if(operate==1){
				 addExaminee();
			 }else if(operate==2){
				 modifyExaminee();
			 }else if(operate==3){
				 deleteExaminee();
			 }else if(operate==4){
				 displayExaminee();
			 }else if(operate==5){
				 displayByCity();
			 }else if(operate==6){
				 displayByName();
			 }else if(operate==7){
				 displayByTicketId();
			 }else if(operate==8){
				 showMainmenu();
				 break;
			 }
		 }
	}
	/**
	 * 显示学生菜单
	 */
	public void showUserMenu(){
		System.out.println("请选择.........");
		while(true){
			printSeparator();
			 System.out.println("1.查询本市的总分状元");
			 System.out.println("2.查询各个省的状元");
			 System.out.println("3.查询指定城市的状元");
			 System.out.println("4.退出");
			 printSeparator();
			 int operate = scanner.nextInt();
			 if(operate==1){
				 
			 }else if(operate==2){
				 
			 }else if(operate==3){
				 
			 }else if(operate==4){ 
				 examineeService.save();
				 System.out.println("谢谢使用");
				 break;
			 }
		}
	}
	
	/**
	 * 录入学生数据
	 */
	public void addExaminee(){
		System.out.println("------------录入考生信息--------");
		while(true){
			Examinee examinee = new Examinee();
			System.out.print("请输入准考证号：");
			String ticketId = scanner.next();
			examinee.setTicketId(ticketId);
			if(examineeService.getExamineeByTicketId(ticketId) != null){
				System.out.println("准考证号已存在请重新输入");
				break;
			}
			System.out.print("请输入密码：");
			String password = scanner.next();
			examinee.setPassword(password);
			System.out.print("请输入姓名:");
			String name = scanner.next();
			examinee.setName(name);
			System.out.print("请输入年龄:");
			int age = scanner.nextInt();
			examinee.setAge(age);
			System.out.print("请输入省份:");
			String province = scanner.next();
			examinee.setProvince(province);
			System.out.print("请输入户籍所在地:");
			String city = scanner.next();
			examinee.setCity(city);
			System.out.print("请输入语文分数:");
			int chineseScore = scanner.nextInt();
			examinee.setChineseScore(chineseScore);
			System.out.print("请输入数学分数:");
			int mathScore = scanner.nextInt();
			examinee.setMathScore(mathScore);
			System.out.print("请输入英语分数:");
			int englishScore = scanner.nextInt();
			examinee.setEnglishScore(englishScore);
			examineeService.add(examinee);
			System.out.println("录入成功！");
			break;
		}
	}
	/**
	 * 修改学生数据
	 */
	public void modifyExaminee(){
		System.out.println("------------修改考生信息--------");
		System.out.println("请输入准考证号:");
		String ticketId = scanner.next();
		scanner.nextLine();
		Examinee examinee = examineeService.getExamineeByTicketId(ticketId);
		if(examinee != null){
			System.out.println("请输入要修改的信息，直接回车表示不修改");
			System.out.print("密码("+examinee.getPassword()+"):");
			String password = scanner.nextLine();
			if(!password.equals("")){
				examinee.setPassword(password);
			}
			System.out.print("姓名("+examinee.getName()+"):");
			String name = scanner.nextLine();
			if(!name.equals("")){
				examinee.setName(name);
			}
			System.out.print("年龄"+(examinee.getAge())+":");
			String age = scanner.nextLine();
			if(!age.equals("")){
				int age1 = Integer.parseInt(age);
				examinee.setAge(age1);
			}
			System.out.print("所属省份("+examinee.getProvince()+"):");
			String province = scanner.nextLine();
			if(!province.equals("")){
				examinee.setProvince(province);
			}
			System.out.print("户籍所在地("+examinee.getCity()+"):");
			String city = scanner.nextLine();
			if(!city.equals("")){
				examinee.setCity(city);
			}
			System.out.print("语文成绩"+(examinee.getChineseScore())+":");
			String chineseScore = scanner.nextLine();
			if(!chineseScore.equals("")){
				int chineseScore1 = Integer.parseInt(chineseScore);
				examinee.setChineseScore(chineseScore1);
			}
			System.out.print("英语成绩"+(examinee.getEnglishScore())+":");
			String englishScore = scanner.nextLine();
			if(!englishScore.equals("")){
				int englishScore1 = Integer.parseInt(englishScore);
				examinee.setEnglishScore(englishScore1);
			}
			System.out.print("数学成绩"+(examinee.getMathScore())+":");
			String mathScore = scanner.nextLine();
			if(!mathScore.equals("")){
				int mathScore1 = Integer.parseInt(mathScore);
				examinee.setMathScore(mathScore1);
			}
		}else{
			System.out.println("准考证号不存在，请重新输入");
			modifyExaminee();
		}
		if(examineeService.modify(examinee)){
			
			System.out.println("修改成功");
		}else{
			System.out.println("修改失败");
		}
	}
	/**
	 * 删除学生数据
	 */
	public void deleteExaminee(){
		
		System.out.println("------------删除考生信息--------");
		System.out.println("请输入准考证号:");
		String ticketId = scanner.next();
		if(examineeService.delete(ticketId)){
			System.out.println("删除成功");
		}else{
			System.out.println("删除失败");
		}
	}
	/**
	 * 显示所有学生数据
	 */
	public void displayExaminee(){
		System.out.println("------------显示考生信息--------");
		System.out.println("准考证号\t姓名\t年龄\t省份\t户籍\t语文分数\t数学分数\t英语分数\t总分");
		for(Examinee examinee :examineeService.getExaminee()){
			System.out.println(examinee);
		}
		
	}
	/**
	 * 根据户籍所在地显示学生数据
	 */
	public void displayByCity(){
		System.out.println("请输入户籍所在地");
		String city = scanner.next();
		List<Examinee>list=examineeService.findByCity(city);
		//isEmpty()：判断集合是否是空集合
		if(!list.isEmpty()){
			for(Examinee examinee : list){
				System.out.println("准考证号\t姓名\t年龄\t省份\t户籍\t语文分数\t数学分数\t英语分数\t总分");
				System.out.println(examinee);
			}
		}else{
			System.out.println("查询失败");
		}
		
	}
	/**
	 * 根据名字查询学生信息
	 */
	public void displayByName(){
		System.out.println("请输入名字");
		String name = scanner.next();
		List<Examinee>list=examineeService.findByName(name);
		if(!list.isEmpty()){
			for(Examinee examinee : list){
				System.out.println("准考证号\t姓名\t年龄\t省份\t户籍\t语文分数\t数学分数\t英语分数\t总分");
				System.out.println(examinee);
			}
		}else{
			System.out.println("查询失败");
		}
	}
	/**
	 * 根据学生的准考证号进行查询
	 */
	public void displayByTicketId(){
		System.out.println("请输入准考证号");
		String ticketId = scanner.next();
		Examinee examinee = examineeService.getExamineeByTicketId(ticketId);
		if(examinee!=null){
			System.out.println("准考证号\t姓名\t年龄\t省份\t户籍\t语文分数\t数学分数\t英语分数\t总分");
			System.out.println(examinee);
		}else{
			System.out.println("查询失败");
		}
	}
	/**
	 * 管理员登录
	 */
	public void adminLogin(){
		Admin admin = new Admin();
		System.out.print("请输入用户名：");
		String userName = scanner.next();
		System.out.print("请输入密码：");
		String password = scanner.next();
		boolean result=admin.login(userName, password);
		if(result){
			System.out.println("登录成功");
			showAdminMenu();
		}else{
			System.out.println("登录失败");
			showMainmenu();
		}
	}
	/**
	 * 考生登录 登录成功后显示考生数据
	 */
	public void userLogin(){
		System.out.print("请输入准考证号：");
		String ticketId = scanner.next();
		System.out.print("请输入密码：");
		String password = scanner.next();
		Examinee examinee = examineeService.login(ticketId, password);
		if(examinee!=null){
			System.out.println("欢迎"+examinee.getName());
			System.out.println("准考证号\t姓名\t年龄\t省份\t户籍\t语文分数\t数学分数\t英语分数\t总分");
			System.out.print(examinee);
			while(true){
				System.out.println("是否修改个人信息 请输入1.是 2.不是");
				int choose = scanner.nextInt();
				if(choose==1){
					scanner.nextLine();
					System.out.println("请输入要修改的信息，直接回车表示不修改");
					examinee=examineeService.getExamineeByTicketId(ticketId);
					System.out.print("密码("+examinee.getPassword()+"):");
					String password1 = scanner.nextLine();
					if(!password1.equals("")){
						examinee.setPassword(password1);
					}
					System.out.print("姓名("+examinee.getName()+"):");
					String name = scanner.nextLine();
					if(!name.equals("")){
						examinee.setName(name);
					}
					System.out.print("年龄"+(examinee.getAge())+":");
					String age = scanner.nextLine();
					if(!age.equals("")){
						int age1 = Integer.parseInt(age);
						examinee.setAge(age1);
					}
					System.out.print("所属省份("+examinee.getProvince()+"):");
					String province = scanner.nextLine();
					if(!province.equals("")){
						examinee.setProvince(province);
					}
					System.out.print("户籍所在地("+examinee.getCity()+"):");
					String city = scanner.nextLine();
					if(!city.equals("")){
						examinee.setCity(city);
					}
					if(examineeService.modify(examinee)){
						
						System.out.println("修改成功");
					}else{
						System.out.println("修改失败");
					}
					showUserMenu();
					break;
				}else if(choose==2){
					showUserMenu();
					break;
				}else{
					break;
				}
				
			}
			
		}else{
			System.out.println("准考证或密码错误，登录失败");
		}
	}
	/**
	 *打印分隔符
	 */
	public void printSeparator(){
		System.out.println("-----------------------------");
	}

}
